package io.babystar.common.utils;
import java.util.Random;

/**
 * 验证码
 *
 * @author ypj
 *
 */
public class CheckCodeUtils {

    /**
     * 取得6位随机不同的数
     */
    public static int random() {
        int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 6; i++) {
            result = result * 10 + array[i];
        }
        return result;
    }

    /**
     * 返回长度为size,随机从A~Z,0~9中选取 的字符组成的字符串
     */
    public static String getCode(int size) {
        String code = "";
        String strs = "ABCDEFGHIJKLMNOPQRST" + "UVWXYZ0123456789";
        Random r = new Random();
        for (int i = 0; i < size; i++) {
            code += strs.charAt(r.nextInt(strs.length()));
        }
        return code;
    }

}

